#!/usr/bin/env python
# -*- coding: utf-8 -*-

def parse_tag_file(file_name):
    results = []
    with open(file_name) as f:
        for line in f:
            line = line.strip()
            if line == "" or line.startswith("#"):
                continue
            sp = line.split()
            word = None
            tag = None
            if len(sp) == 1:
                tag = 100
                word = sp[0]
            else:
                try:
                    tag = int(sp[-1])
                    word = " ".join(sp[:-1])
                except ValueError:
                    tag = 100
                    word = line
                    # raise
            results.append((word, tag))
    return results

def format_tag_map(results, out_file, max, min):
    with open(out_file, "w") as ofs:
        tag_map ={}
        for item in results:
            tag = item[1]
            word = item[0]
            if not tag_map.has_key(tag):
                tag_map[tag] = 0
            tag_map[tag] += 1
            if tag < max and tag >= min:
                ofs.write("%s\n"%(word))
    
        for tag in tag_map:
            print("%d: %d"%(tag, tag_map[tag]))
    
def main():
    file_path = "/home/long/work/easy-note/tmp/english/yjs-english-abstruct-word.md"
    results = parse_tag_file(file_path)
    min = 0
    max = 5
    format_tag_map(results, "%d-%d-yjs.txt"%(min, max), max, min)
    
if __name__ == "__main__":
    main()